package cn.saas.testcc;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import cn.saas.testcc.greendao.gen.OrdersDao;
import cn.saas.testcc.greendao.gen.UserDao;

public class MainActivity extends AppCompatActivity {

    private UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DbManager.getInstance().initGreenDao(this, "aaaaa");

        userDao = DbManager.getInstance().getDaoSession().getUserDao();

        insertMany();
        insertOneToMany();
        queryList() ;

        TextView mTv = (TextView) findViewById(R.id.tv);

        mTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                queryToManyUserToOrder();
            }
        });
    }


    private void insertMany() {
        List<User> users = new ArrayList<>();

        User user1 = new User();
        user1.setName("孙悟空");
        user1.setUsercode("001");
        user1.setUserAddress("花果山水帘洞");

        User user2 = new User();
        user2.setName("猪八戒");
        user2.setUsercode("002");
        user2.setUserAddress("高老庄");

        User user3 = new User();
        user3.setName("沙悟净");
        user3.setUsercode("003");
        user3.setUserAddress("流沙河");

        User user4 = new User();
        user4.setName("黑熊怪");
        user4.setUsercode("004");
        user4.setUserAddress("黑风山");

        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);

        userDao.insertInTx(users);
    }

    // 打印数据库表中的记录
    private void queryList() {
        String result = "显示结果为：";
        // 获得表中的所有记录
        List<User> users = userDao.loadAll();
        int i = 0;
        for (User user : users) {
            i++;
            String res = result + "i=" + i + ",id:" + user.getId() + ",name:" + user.getName() + ",address:" + user.getUserAddress();
            Log.d("TAG", res);
        }
    }


    private void insertOneToMany() {
        List<Orders> orderList = new ArrayList<Orders>();
        // 这些数据的来源请参考上一章所讲的内容，因为在上一章中有方法为测试提供数据源
        UserDao userDao = DbManager.getInstance().getDaoSession().getUserDao();
        OrdersDao ordersDao = DbManager.getInstance().getDaoSession().getOrdersDao();
        User user1 = userDao.queryBuilder().where(UserDao.Properties.Name.eq("孙悟空")).build().unique();
        User user2 = userDao.queryBuilder().where(UserDao.Properties.Name.eq("猪八戒")).build().unique();

        Orders order1 = new Orders();
        order1.setGoodsName("金箍棒");
        order1.setUser(user1); //设置外键值时，要用setUser()方法，以确保外键值不会出错

        Orders order2 = new Orders();
        order2.setGoodsName("黄金甲");
        order2.setUser(user1);

        Orders order3 = new Orders();
        order3.setGoodsName("紫金冠");
        order3.setUser(user1);

        Orders order4 = new Orders();
        order4.setGoodsName("紫金冠");
        order4.setUser(user2);

        orderList.add(order1);
        orderList.add(order2);
        orderList.add(order3);
        orderList.add(order4);

        ordersDao.insertInTx(orderList);
    }

    private void queryToManyUserToOrder() {
        OrdersDao ordersDao = DbManager.getInstance().getDaoSession().getOrdersDao();


        List<Orders> ordersList;
        UserDao userDao = DbManager.getInstance().getDaoSession().getUserDao();
        User user1 = userDao.queryBuilder().where(UserDao.Properties.Name.eq("孙悟空")).build().unique();
        ordersDao.insert(order(user1));

        //直接通过User对象的getOrders()方法获得此用户的所有订单
        ordersList = user1.getOrders();
        Log.d("aaaaa", user1.getName() + "的订单内容为：");

        int i = 0;
        if (ordersList != null) {
            for (Orders order : ordersList) {
                i = i + 1;
                Log.d("aaaaa", "第" + i + "条订单的结果：" + ",id:" + order.getId()
                        + ",商品名：" + order.getGoodsName()
                        + ",用户名：" + user1.getName());
            }
        }
    }

    private Orders order(User user1) {
        Orders order5 = new Orders();
        order5.setGoodsName("哈哈");
        order5.setUser(user1);
        return order5;
    }


}